package com.xiguthings.waka.publicUtils;

import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

public class StringUtils extends org.apache.commons.lang3.StringUtils
{

    private static final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    /**
     * 根据分隔符号，将字符串分隔成一个list集合
     *
     * @param str
     * @param splitRegex
     * @return
     */
    public static List<String> listBySplit(String str, String splitRegex) {
        if (StringUtils.isBlank(str) || StringUtils.isBlank(splitRegex)) {
            return null;
        }
        String[] strArr = str.split(splitRegex);
        List<String> list = new ArrayList<>(Arrays.asList(strArr));
        return list;
    }

    public static String getRandomUUID(){
    	return UUID.randomUUID().toString().replace("-", "");
    }
    public static String parseDateToString(Date date)
    {
        if (date == null)
        {
            return "";
            // date = new Date();
        }
        return df.format(date);
    }

    public static String parseDateToString(Date date, String pattern)
    {
        if (date == null)
        {
            return null;
        }
        DateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    public static Date parseStringToDate(String dateStr)
    {
        try
        {
            return df.parse(dateStr);
        } catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }

    public static Integer parseInt(String str)
    {
        try
        {
            return Integer.valueOf(str);
        } catch (Exception e)
        {
            return null;
        }

    }
    public static Long parseLong(String str)
    {
        try
        {
            return Long.valueOf(str);
        } catch (Exception e)
        {
            return null;
        }

    }

    public static Boolean parseBoolean(String str)
    {
        try
        {
            return Boolean.valueOf(str);
        } catch (Exception e)
        {
            return null;
        }
    }

    public static Date parseStrToSimpleDate(String str)
    {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try
        {
            return df.parse(str);
        } catch (ParseException e)
        {

            return null;
        }
    }

    public static String parseSimpleDateToStr(Date date)
    {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try
        {
            return df.format(date);
        } catch (Exception e)
        {

            return null;
        }
    }

    public static String toLinuxPath(String path)
    {
        if (isBlank(path))
        {
            return "";
        }
        return path.replaceAll("\\\\", "/").replaceAll("\\\\", "/").replaceAll("//", "/").replaceAll("//", "/");
    }

    public static String getIpAddr(HttpServletRequest request)
    {
        String userIp = request.getHeader("x-forwarded-for");
        if (userIp == null || userIp.length() == 0 || "unknown".equalsIgnoreCase(userIp))
        {
            userIp = request.getHeader("Proxy-Client-IP");
        }
        if (userIp == null || userIp.length() == 0 || "unknown".equalsIgnoreCase(userIp))
        {
            userIp = request.getHeader("WL-Proxy-Client-IP");
        }
        if (userIp == null || userIp.length() == 0 || "unknown".equalsIgnoreCase(userIp))
        {
            userIp = request.getRemoteAddr();
        }
        return userIp;
    }

    /**
     * 
     * 获取随机字符串
     * <p>
     * Description: TODO(详细描述) <br/>
     * <p>
     * Author: Administrator<br/>
     * <p>
     * Date: 2014-5-29-下午3:52:41<br/>
     * <p>
     * 
     * @param len
     * @return
     * 
     */
    public static String genRandomString(int len)
    {
        String randString = "0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$";
        Random r = new Random();
        String s = "";
        for (int i = 0; i < len; i++)
        {
            s += randString.charAt(r.nextInt(63));
        }
        return s;

    }

    public static Double parseDouble(String str)
    {
        try
        {
            return Double.valueOf(str);
        } catch (Exception e)
        {
            return null;
        }
    }

    public static String stringArr2String(String[] arr, String split)
    {
        if (arr == null || arr.length == 0)
        {
            return "";
        }
        String str = "";
        for (int i = 0; i < arr.length; i++)
        {
            if (i != arr.length - 1)
            {
                str = str + arr[i] + split;
            } else
            {
                str = str + arr[i];
            }
        }
        return str;
    }

    public static String genReqParam(HttpServletRequest request)
    {
        Map<String, String[]> reqParam = request.getParameterMap();
        StringBuffer sb = new StringBuffer();

        if (reqParam != null && reqParam.size() > 0)
        {
            int index = 0;
            int size = reqParam.size();
            for (Entry<String, String[]> en : reqParam.entrySet())
            {
                sb.append(en.getKey()).append("=").append(StringUtils.stringArr2String(en.getValue(), ","));

                if (index != size - 1)
                {
                    sb.append("&");
                }
                index++;
            }
        }
        return sb.toString();
    }

    public static String execDownFileName(String fileName, boolean isIE)
    {
        try
        {
            // 判断是否为IE浏览器
            if (isIE)
            {
                fileName = URLEncoder.encode(fileName, "UTF-8");
            } else
            {
                fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
            }

        } catch (Exception e)
        {
        }
        return fileName;
    }
    
    public static String longDoubleToString(Double d){
        DecimalFormat format =  new DecimalFormat("#.########");
//        String fmatStr = format.format(d);
//        if(fmatStr.indexOf("000") == (fmatStr.lastIndexOf(".")+1)){
//            DecimalFormat format2 = new DecimalFormat("#");
//            return format2.format(d);
//        }
        
        
        
        return format.format(d);
    }
    public static void main(String[] args)
    {
        System.out.println(longDoubleToString(15874.560));
    }


}
